package com.ruoyi.firefighterTrainingTable.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.firefighter.domain.FirefighterInfo;
import com.ruoyi.firefighter.service.IFirefighterInfoService;
import com.ruoyi.firefighterTrainingTable.domain.FirefighterTrainingTable;
import com.ruoyi.firefighterTrainingTable.service.IFirefighterTrainingTableService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.stream.Collectors;


import com.ruoyi.common.core.domain.entity.SysDept;

import com.ruoyi.ifmEnlistIand.service.ISysDeptService;
import org.springframework.remoting.RemoteTimeoutException;
/**
 * 体能科目训练登记Controller
 *
 * @author ruoyi
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/system/FirefighterTrainingTable")
public class FirefighterTrainingTableController extends BaseController
{
    @Autowired
    private IFirefighterTrainingTableService firefighterTrainingTableService;
    @Autowired
    private IFirefighterInfoService iFirefighterInfoService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IFirefighterInfoService firefighterInfoService;


    /**
     * 后端根据架构查询当前部职别及其子集架构
     *
     *
     *
     */
    public List<FirefighterInfo> GetzUnit(  String deptId1){
        if(deptId1==null){
            throw new RemoteTimeoutException("值不能为空");
        }
        Long deptId =  Long.parseLong(deptId1);

        List<SysDept> sysDepts= sysDeptService.selectDeptListbyAncestors(deptId);
        HashSet<Integer> result = new HashSet<>();
        for (SysDept sysDept : sysDepts) {
            result.add(sysDept.getDeptId().intValue());
            String ancestors = sysDept.getAncestors();
            String[] parts = ancestors.split(",");
            boolean startFound = false;
            for (String part : parts) {
                if (startFound) {
                    result.add(Integer.parseInt(part));
                } else if (part.equals(deptId.toString())) {
                    startFound = true;
                }
            }
        }
        Integer deptIdInt = deptId != null ? deptId.intValue() : 0;
        result.add(deptIdInt);
        ArrayList<Integer> a1 = new ArrayList<>();
        for (Integer integer : result) {
            a1.add(integer);
        }
        List<SysDept> sysDeptslist=  sysDeptService.selectListbyb(a1);
        FirefighterInfo firefighterInfo = new FirefighterInfo();
        List<FirefighterInfo> list = firefighterInfoService.selectFirefighterInfoList(firefighterInfo);
        ArrayList<FirefighterInfo> moreFilterAS = new ArrayList<>();
        for (SysDept dept : sysDeptslist) {
            for (FirefighterInfo filterA : list) {
                if (dept.getDeptName().equals(filterA.getUnit())) {
                    moreFilterAS.add(filterA);
                }
            }
        }
        return moreFilterAS;
    }
    /**
     * 查询体能科目训练登记列表
     */
    @PreAuthorize("@ss.hasPermi('system:FirefighterTrainingTable:list')")
    @GetMapping("/list")
    public TableDataInfo list(FirefighterTrainingTable firefighterTrainingTable)
    {
        LoginUser loginUser = getLoginUser();
        String string = loginUser.getDeptId().toString();
        //登录人部职别ID
        List<FirefighterInfo> list1 = GetzUnit(string);
        ArrayList<FirefighterTrainingTable> firefighterInfos = new ArrayList<>();
        List<FirefighterTrainingTable> list = firefighterTrainingTableService.selectFirefighterTrainingTableList(firefighterTrainingTable);
        for (FirefighterTrainingTable info : list) {
            for (FirefighterInfo firefighterInfo1 : list1) {
                if (info.getUserId()!=null&&info.getUserId().equals(firefighterInfo1.getUserId())) {
                    firefighterInfos.add(info);
                }
            }
        }
        Map<String, Integer> stringIntegerMap = startPage1();
        Integer pageNum = stringIntegerMap.get("pageNum");
        Integer pageSize = stringIntegerMap.get("pageSize");
        List<FirefighterTrainingTable> paginatedList = firefighterInfos.stream()
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        return getDataTable1(paginatedList,firefighterInfos.size());
    }

    /**
     * 导出体能科目训练登记列表
     */
    @PreAuthorize("@ss.hasPermi('system:FirefighterTrainingTable:export')")
    @Log(title = "体能科目训练登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FirefighterTrainingTable firefighterTrainingTable)
    {
        List<FirefighterTrainingTable> list = firefighterTrainingTableService.selectFirefighterTrainingTableList(firefighterTrainingTable);
        ExcelUtil<FirefighterTrainingTable> util = new ExcelUtil<FirefighterTrainingTable>(FirefighterTrainingTable.class);
        util.exportExcel(response, list, "体能科目训练登记数据");
    }

    /**
     * 获取体能科目训练登记详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:FirefighterTrainingTable:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(firefighterTrainingTableService.selectFirefighterTrainingTableById(id));
    }

    /**
     * 新增体能科目训练登记
     */
    @PreAuthorize("@ss.hasPermi('system:FirefighterTrainingTable:add')")
    @Log(title = "体能科目训练登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FirefighterTrainingTable firefighterTrainingTable)
    {
        FirefighterInfo firefighterInfo1 = new FirefighterInfo();
        firefighterInfo1.setIdCardNumber(firefighterTrainingTable.getFirefighterIdCard());
        FirefighterInfo firefighterInfo= iFirefighterInfoService.selectFirefighterInfoBySfz(firefighterInfo1);
        firefighterTrainingTable.setUserId(firefighterInfo.getUserId());
        return toAjax(firefighterTrainingTableService.insertFirefighterTrainingTable(firefighterTrainingTable));
    }

    /**
     * 修改体能科目训练登记
     */
    @PreAuthorize("@ss.hasPermi('system:FirefighterTrainingTable:edit')")
    @Log(title = "体能科目训练登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FirefighterTrainingTable firefighterTrainingTable)
    {
        return toAjax(firefighterTrainingTableService.updateFirefighterTrainingTable(firefighterTrainingTable));
    }

    /**
     * 删除体能科目训练登记
     */
    @PreAuthorize("@ss.hasPermi('system:FirefighterTrainingTable:remove')")
    @Log(title = "体能科目训练登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(firefighterTrainingTableService.deleteFirefighterTrainingTableByIds(ids));
    }
}
